package console

import (
	"github.com/goravel/framework/contracts/console"
	"github.com/goravel/framework/contracts/schedule"
	"github.com/goravel/framework/facades"
	"goravel/app/models"
)

type Kernel struct {
}

func (kernel *Kernel) Schedule() []schedule.Event {
	return []schedule.Event{
		facades.Schedule().Call(func() {
			var deviceStates []models.Devicestate
			facades.Orm().Query().Table("devicestates as a").
				Select("a.*").
				Join(`INNER JOIN (
			SELECT device_id, MAX(created_at) as created_at
			FROM devicestates
			GROUP BY device_id
			) as sub ON a.device_id = sub.device_id AND a.created_at = sub.created_at`).
				Scan(&deviceStates)
			//	将deviceStates的状态设置为2，下线，每60秒
			for _, state := range deviceStates {
				state.State = 2
				facades.Orm().Query().Model(&models.Devicestate{}).Where("id = ?", state.ID).Update("state", 2)
			}
		}).EveryMinute(),
	}
}

func (kernel *Kernel) Commands() []console.Command {
	return []console.Command{}
}
